package me.raih.service.impl;

import me.raih.mapper.BillMapper;
import me.raih.pojo.po.Bill;
import me.raih.pojo.vo.PageBean;
import me.raih.service.BillService;
import me.raih.util.WaresUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author Raih
 * @Time 2021/1/5 18:19
 */
@Service
@Transactional
public class BillServiceImpl implements BillService {

    @Autowired
    BillMapper billMapper;

    @Autowired
    PageBean<Bill> pageBean;

    @Override
    public boolean canCreateBill(Bill bill) {

        int i = billMapper.addBill(bill);
        if(i == 1){
            return true;
        }
        return false;
    }

    @Override
    public PageBean<Bill> findBillByBillNoAndPageAndUsername(String billNo, int currentPage, int bigPage, String param, int pageSize ,String username) {

        if(billNo != null && billNo.length() != 0){
            billNo = "%"+billNo+"%";
        }else {
            billNo = null;
        }
        if(username != null && username.length() != 0){
            username = "%"+username+"%";
        }else {
            username = null;
        }
        List<Bill> billList = new ArrayList<>();
        int max = 0;
        //必定是首次查询
        if("search".equals(param)){
            currentPage = 1;
        }else if("nextPage".equals(param)){
            //查找下一页
            if(currentPage >= bigPage){
                currentPage = bigPage;
            }else {
                currentPage ++;
            }
        }else if("begin".equals(param)){
            currentPage = 1;
        }else if("forward".equals(param)){
            //查找上一页
            if(currentPage == 1){
            }else {
                currentPage --;
            }
        }else if("end".equals(param)){
            //最后一页
            currentPage = bigPage;
        }
        billList = billMapper.findBillByBillNoAndPageAndUsername(billNo,(currentPage-1)*pageSize,pageSize,username);
        max = billMapper.findBillByBillNoAndPageAndUsernameNumber(billNo,(currentPage-1)*pageSize,pageSize,username);
        //遍历所有订单，将string转换成map
        for (Bill bill : billList) {
            String sellWaresMapStr = bill.getSellWaresMapStr();
            Map<String, Integer> map = WaresUtil.caseMap(sellWaresMapStr);
            bill.setSellWaresMap(map);
        }

        //页面数据设置
        pageBean.setList(billList);
        pageBean.setTotalPage(max);
        pageBean.setPageSize(pageSize);
        pageBean.setCurrentPage(currentPage);
        pageBean.setTotalCount(max%pageSize == 0?(max/pageSize):((max/pageSize)+1));


        return pageBean;
    }

    @Override
    public List<Bill> findBillByUsernameAndTime(String username, int time) {

        //billMapper.findBillByMonth(time,username);


        return null;
    }

    @Override
    public PageBean<Bill> findBillByBillNoAndPageAndUsernameAndSalesName(String billNo, int currentPage, int bigPage, String param, int pageSize, String username, String salesName) {

        if(billNo != null && billNo.length() > 0){
            billNo = "%"+billNo+"%";
        }else {
            billNo = null;
        }
        if(username != null && username.length() > 0){
            username = "%"+username+"%";
        }else {
            username = null;
        }
        if(salesName != null && salesName.length() > 0){
            salesName = "%"+salesName+"%";
        }else {
            salesName = null;
        }
        //首次查询
        if("search".equals(param)){
            currentPage = 1;
        }else if("begin".equals(param)){
            //去首页时候
            currentPage = 1;
        }else if("forward".equals(param)){
            //上一页时候
            if(currentPage > 1){
                currentPage --;
            }
        }else if("nextPage".equals(param)){
            //下一页时候
            if(currentPage < bigPage){
                currentPage ++;
            }
        }else if("end".equals(param)){
            currentPage = bigPage;
        }else {
            currentPage = 1;
        }

        List<Bill> billList = billMapper.findBillByBillNoAndPageAndUsernameAndSales(billNo, (currentPage - 1) * pageSize, pageSize, username, salesName);
        int max = billMapper.findBillByBillNoAndPageAndUsernameAndSalesNumber(billNo, (currentPage - 1) * pageSize, pageSize, username, salesName);
        //遍历所有订单，将string转换成map
        for (Bill bill : billList) {
            String sellWaresMapStr = bill.getSellWaresMapStr();
            Map<String, Integer> map = WaresUtil.caseMap(sellWaresMapStr);
            bill.setSellWaresMap(map);
        }
        pageBean.setCurrentPage(currentPage);
        pageBean.setList(billList);
        pageBean.setPageSize(pageSize);
        pageBean.setTotalCount(max);
        pageBean.setTotalPage((max%pageSize==0)?(max/pageSize):((max/pageSize)+1));

        return pageBean;
    }
}
